<?php
require 'config/config.php';
$account  = getstr('account');
$password = getstr('password');
$sign     = getstr('sign');
$ip       = getip();
$datetime = date('Y-m-d H:i:s');
$regex    = '/^[A-Za-z0-9]+$/i';
// 验证签名
$mymd5key = getkey($account, $password);
if ($sign == $mymd5key) {
 // 开始登陆或者注册
 $sql    = "SELECT * FROM user WHERE account='{$account}'";
 $result = $pdo->query($sql);
 $row    = $result->rowCount();
 $row2   = $result->fetchAll();
 if ($row > 0) {
  if ($row2[0]['password'] == $password) {
   $sqlupdate = "UPDATE user SET lasttime='{$datetime}' WHERE (account='{$account}')";
   $pdo->query($sqlupdate);
   $data = array(
    'ret' => 0,
   );
  } else {
   $data = array(
    'ret' => 3,
   );
  }
 } else {
  if (preg_match($regex, $account)) {
   if (preg_match($regex, $password)) {
    $sql    = "INSERT INTO user (`account`, `password`, `regtime`, `regip`, `lasttime`) VALUES ('{$account}', '{$password}', '{$datetime}', '{$ip}', '{$datetime}');";
    $result = $pdo->query($sql);
    if ($result) {
     $data = array(
      'ret' => 0,
     );
    } else {
     $data = array(
      'ret' => 2,
     );
    }
   } else {
    $data = array(
     'ret' => 5,
    );
   }
  } else {
   $data = array(
    'ret' => 4,
   );
  }
 }
} else {
 $data = array(
  // 签名错误
  'ret' => 8,
 );
}
echo json_encode($data);
